import { Controller, Get,Body,BadRequestException, Post, Delete,Param,ParseIntPipe } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { success } from '@/utils';
@Controller('categories')
export class CategoriesController {
    constructor(private readonly categoriesService: CategoriesService) {}
    @Post('list')
    async getList(@Body() info:any) {
        try {   
            const res =await this.categoriesService.findAll(info);
            return success(res, '获取成功');
          } catch (error) {
              throw new BadRequestException('参数不正确');
          }
    }
    @Post('listBy')
    async listBy(@Body() info:any) {
        try {   
            const res =await this.categoriesService.findBy(info);
            return success(res, '获取成功');
          } catch (error) {
            throw new BadRequestException('参数不正确');
        }
        
    }
    @Post('edit')
    async edit(@Body() info:any) {
        let res :any
        try {  
            if(!info.id){
                res =await this.categoriesService.create(info);
            }else{
                res =await this.categoriesService.update(info.id,info);
            }
           
            return success(res, '获取成功');
          } catch (error) {
            throw new BadRequestException('参数不正确');
        }
        
    }
    @Get('/detail/:id')
    async getDetail(@Param('id',ParseIntPipe) id: number) {
        try {   
            const res =await this.categoriesService.findOne(id);
            return success(res, '获取成功');
          } catch (error) {
              throw new BadRequestException('参数不正确');
          }
        
    }
}
